package com.etc.controller;

import com.etc.common.MyException;
import com.etc.po.UserMast;
import com.etc.service.IUserMastService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("jsp/user")
public class UserMastController {

    // 动态代理注入
    @Autowired
    IUserMastService service;

    @RequestMapping("/search.action")
    public String search(Model model) {
        // 调用service方法
        List<UserMast> userlist = service.findAll();
        model.addAttribute("userlist", userlist);
        return "user/usersearch";
    }


    // jsp/user/adduname.action
    @RequestMapping("adduname")
    @ResponseBody
    public HashMap testtrancational(String uname) {

        HashMap map = new HashMap();

        try {
            service.add(uname);
        } catch (MyException e) {
            e.printStackTrace();
            map.put("code", "500");
            map.put("message", "失败了");
            return map;
        }
        map.put("code", "200");
        map.put("message", "成功了");
        return map;
    }
}
